﻿using DataBase.Utils;
using GreenXi.Help.Curve;
using GreenXi.Pages.index;
using GreenXi.Pages.Navig;
using GreenXi.ViewModel;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using ZedGraph;
using static GreenXi.Help.Curve.SmoothChart;

namespace GreenXi.Pages.Power
{
    /// <summary>
    /// PressureCurve.xaml 的交互逻辑
    /// </summary>
    public partial class PressureCurve : System.Windows.Controls.UserControl
    {
        public static ZedGraph.ZedGraphControl ZedGraphContro11;
        public static ZedGraph.ZedGraphControl ZedGraphControl2;

        public static PressureCurveViewModel Instance { get; set; }

        public static Double X1 = 0;
        public static Double X2 = 0;
        public static Double Y1 = 0;
        public static Double Y2 = 0;
        public static int ID = 0;

        //记录当前点xy坐标
        private static double oldx = 0;
        private static double oldy = 0;

        //点击的第几个框
        private static int Num = 0;

        //点击的是节点还是内部
        private static int node = 0;

        //鼠标拖拽状态
        private bool isDragging = false;
        private bool isResizing = false;
        private System.Drawing.Point mousePosition;

        public PressureCurve()
        {
            InitializeComponent();
            Window_Loaded();
            Instance = new PressureCurveViewModel(this);

            this.DataContext = Instance;
            zedGraphControl1.IsShowPointValues = true;

            // 注册MouseDownEvent事件，以捕获用户选择或操作图形对象的事件
            //    zedGraphControl.PointValueEvent += new ZedGraphControl.PointValueHandler(MyPointValueHandler);
         
        }

        //鼠标点击
        public bool ZedMouseEventHandler(ZedGraphControl sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // 检查鼠标位置是否在图形对象内

                // 将屏幕坐标转换为图像坐标
                sender.GraphPane.ReverseTransform(new PointF(e.X, e.Y), out oldx, out oldy);
                Num = isinside(sender.GraphPane.CurveList, oldx, oldy);
                if (Num > 0) //点击在判断框内
                {
                    isDragging = true;

                    //UpMenu2.Visibility = Visibility.Visible;
                    //UpMenu.Visibility = Visibility.Collapsed;
                    x1.Text = X1.ToString();
                    x2.Text = X2.ToString();
                    y1.Text = Y1.ToString();
                    y2.Text = Y2.ToString();
                }
                else
                {
                    //UpMenu2.Visibility = Visibility.Collapsed;
                    //UpMenu.Visibility = Visibility.Visible;
                }
            }
            return false;
        }

        public void Window_Loaded()
        {
            ZedGraphContro11 = zedGraphControl1;
            Init(ZedGraphContro11, "1", GlobalVariables.Cylinder1No, GlobalVariables.ListPress);
        }

        /// <summary>
        /// 刷新图表
        /// <paramref name="num">第几个压机</paramref>
        /// </summary>
        public static void AxisChange(int num)
        {
            if (num == 1)
            {
                if (ZedGraphContro11 != null)
                {
                    ZedGraphContro11.AxisChange();
                    ZedGraphContro11.Invalidate();
                }
            }
            if (num == 2)
            {
                if (ZedGraphControl2 != null)
                {
                    ZedGraphControl2.AxisChange();
                    ZedGraphControl2.Invalidate();
                }
            }
        }

        /// <summary>
        /// 上方菜单展开
        /// </summary>
        private void ButtonUpMenu_Click(object sender, MouseButtonEventArgs e)
        {
            if (UpMenu.Visibility == Visibility.Collapsed && UpMenu2.Visibility == Visibility.Collapsed)
            {
                UpMenu.Visibility = Visibility.Visible;
            }
            else
            {
                UpMenu.Visibility = Visibility.Collapsed;
                UpMenu2.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 错误日志展开
        /// </summary>
        private void ButtonDownMenu_Click(object sender, MouseButtonEventArgs e)
        {
            if (DownMenu.Visibility == Visibility.Collapsed) //展开
            {
                DownMenu.Visibility = Visibility.Visible;
            }
            else //折叠
            {
                DownMenu.Visibility = Visibility.Collapsed;
            }
        }

        //鼠标松开
        private bool zedGraphControl1_MouseUpEvent(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDragging = false;
            return isDragging;
        }

        public string MyPointValueHandler(ZedGraphControl sender, GraphPane pane, CurveItem curve, int iPt)
        {
            return "";
        }

        //判断一个点是否在矩形内部
        public static int isinside(CurveList curveitems, double x, double y)
        {
            //4个一循环
            int count = 0;
            for (int i = 0; i <= curveitems.Count; i++)
            {
                count++;
                if (count == 4)
                {
                    count = 0;
                    var points = curveitems[i - 3].Points;
                    X1 = 0;
                    X2 = 0;
                    Y1 = 0;
                    Y2 = 0;

                    X1 = points[0].X;
                    Y1 = points[0].Y;
                    X2 = points[1].X;
                    Y2 = curveitems[i - 2].Points[1].Y;
                    ID = Convert.ToInt32(curveitems[i - 3].Link.Title);
                    node = 0;
                    if (Math.Abs(x - X1) < 5)
                    {
                        if (Math.Abs(y - Y1) < 5)
                        {
                            node = 1;
                        }
                        if (Math.Abs(y - Y2) < 5)
                        {
                            node = 4;
                        }
                    }
                    if (Math.Abs(x - X2) < 5)
                    {
                        if (Math.Abs(y - Y1) < 5)
                        {
                            node = 2;
                        }
                        if (Math.Abs(y - Y2) < 5)
                        {
                            node = 3;
                        }
                    }

                    if ((x > X1 && x < X2 && y > Y1 && y < Y2) || node != 0)
                    {
                        return (i + 1) / 4;
                    }
                }
            }
            return 0;
        }

        //确定，重新加载判断框
        private void Enter_Click(object sender, RoutedEventArgs e)
        {
            X1 = Convert.ToDouble(x1.Text);
            X2 = Convert.ToDouble(x2.Text);
            Y1 = Convert.ToDouble(y1.Text);
            Y2 = Convert.ToDouble(y2.Text);

            //更新判断框 保存进数据库
            DataBase.Decision decision = new DataBase.Decision();
            decision.X1 = X1;
            decision.X2 = X2;
            decision.Y1 = Y1;
            decision.Y2 = Y2;
            decision.ID = ID;
            MyDbContext.Instance.DecisionUpdate(decision);

            ZedGraph.GraphPane myPane = zedGraphControl1.GraphPane;
            myPane.CurveList.Clear();

            var list = MyDbContext.Instance.GetDecision("1"); //获取压机1的判断框
            foreach (var item in list)
            {
                double[] x = { item.X1, item.X2, item.X2, item.X1, item.X1 };
                double[] y = { item.Y1, item.Y1, item.Y2, item.Y2, item.Y1 };
                DecusionType type = new DecusionType();
                #region 判断类型后期从数据库获得
                var decisiontype = MyDbContext.Instance.GetDecisionType(item.Type);
                if (decisiontype != null)
                {
                    type.BottomType = (DashStyle)decisiontype.Bottom;
                    type.TopType = (DashStyle)decisiontype.Top;
                    type.LeftType = (DashStyle)decisiontype.Left;
                    type.RightType = (DashStyle)decisiontype.Right;
                }
                #endregion
                SmoothChart.SmoothChartDemo(myPane, x, y, type, item.ID);
            }
            zedGraphControl1.AxisChange();
            zedGraphControl1.Refresh();
        }

        private void AddDecision_Click(object sender, RoutedEventArgs e)
        {
            Decision decision = new Decision();
            decision.Closed += AddDecision_Closed;
            decision.ShowDialog();
        }

        private void AddDecision_Closed(object sender, EventArgs e)
        {
            Window_Loaded();
        }

        //private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    ZedHeight.Height = new GridLength(this.ActualWidth * 0.25, GridUnitType.Pixel);
        //}

		private void PressureCurveLoaded(object sender, RoutedEventArgs e)
		{
            // 在这里初始化
            GlobalVariables.IsPrcStop1 = false;
			GlobalVariables.IsPrcStop2 = false;
           
		}

        private void GroupBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            try
            {
                if (sender is System.Windows.Controls.GroupBox groupBox)
                {
                   
                    GlobalVariables.CylinderNo = Convert.ToInt32(groupBox.Uid);

                    // Create a new instance of the ProductionData user control
                    var newUserControl = new RealTimeData();

                    // Clear the StackPanel and add the new user control
                    MainWindow._StackPanelMain.Children.Clear();
                    MainWindow._StackPanelMain.Children.Add(newUserControl);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
